using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BackgroundParallax : MonoBehaviour
{
    // Start is called before the first frame update
    public float fparallaxScale = 0.2f;
    public float factor = 0.2f;
    public Transform[] backgrounds;
    public float smooth = 6;
    private Transform camTran;
    private Vector3 camprePos;
    

    void Start()
    {
        camTran = Camera.main.transform;
        camprePos = camTran.position;
    }

    // Update is called once per frame
    void Update()
    {
        float fparallax = (camprePos.x - camTran.position.x) * fparallaxScale;
        for(int i=0;i<backgrounds.Length; i++)
        {
            float fNewX = backgrounds[i].position.x + (1 + factor * i) * fparallax;
            Vector3 newPos = new Vector3(fNewX, backgrounds[i].position.y, backgrounds[i].position.z);
            backgrounds[i].position = Vector3.Lerp(backgrounds[i].position, newPos, smooth * Time.deltaTime);
        }
        camprePos=camTran.position;
    }
}
